package com.eqdd.yiqidian.ui.tongxunlu;

import android.content.DialogInterface;
import android.databinding.DataBindingUtil;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.ImageView;

import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.SearchQunActivityCustom;
import com.eqdd.yiqidian.adapter.recycleadapter.BaseQuickAdapter;
import com.eqdd.yiqidian.adapter.recycleadapter.BaseViewHolder;
import com.eqdd.yiqidian.adapter.recycleadapter.ItemClickSupport;
import com.eqdd.yiqidian.base.BaseActivity;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.bean.rebuild.SearchGroupResultBean;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.http.HttpPresneter;
import com.eqdd.yiqidian.utils.ImageUtil;
import com.eqdd.yiqidian.utils.ToastUtil;
import com.jakewharton.rxbinding.widget.RxTextView;

import java.util.List;
import java.util.concurrent.TimeUnit;

import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class SearchQunActivity extends BaseActivity {
    private SearchQunActivityCustom dataBinding;
    private Subscription subscription;
    private List<SearchGroupResultBean> groupBeanList;
    private BaseQuickAdapter<SearchGroupResultBean, BaseViewHolder> quickAdapter;
    private AlertDialog alertDialog;
    private int clickItemPosition;

    @Override
    public void initBinding() {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_search_qun);
        initTopTitleBar(View.VISIBLE, "搜索群");
    }

    @Override
    public void initData() {

        initDialog();
    }

    private void initDialog() {
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setMessage("确认加群吗?");
            builder.setTitle("提示");
            builder.setPositiveButton("确认", (dialog, which) -> {
                dialog.dismiss();
                new HttpPresneter.Builder(HttpConfig.ADD_GROUP, String.class, SearchQunActivity.this)
                        .Params("userGuid", ";"+user.getGuid())
                        .Params("Groupid", quickAdapter.getItem(clickItemPosition).getGroupid())
                        .Params("GroupName", quickAdapter.getItem(clickItemPosition).getGroupname())
                        .setOnHttpListener(new HttpPresneter.OnHttpListener() {
                            @Override
                            public void onError(Throwable e) {

                            }

                            @Override
                            public void onNext(Object o) {
                                if (((String) o).contains("200")) {
                                    ToastUtil.showShort("你已成功加入该群");
                                    setResult(Config.CHANGE);
                                }
                            }
                        })
                        .build()
                        .post();
            });
            builder.setNegativeButton("取消", (DialogInterface dialog, int which) -> {
                        dialog.dismiss();
                    }
            );
            alertDialog = builder.create();

    }

    @Override
    public void setView() {
        dataBinding.recyclerView.setLayoutManager(new LinearLayoutManager(this));
        dataBinding.recyclerView.setHasFixedSize(true);
        subscription = RxTextView.textChanges(dataBinding.etSearch)
                .debounce(400, TimeUnit.MILLISECONDS, AndroidSchedulers.mainThread())
                .subscribeOn(AndroidSchedulers.mainThread())// 对etKey[EditText]的监听操作 需要在主线程操作
                //对用户输入的关键字进行过滤
                .filter((charSequence) ->
                        charSequence.toString().trim().length() > 0)
                .flatMap((charSequence) ->
                        new HttpPresneter.Builder(HttpConfig.SEARCH_GROUP, SearchGroupResultBean.class, SearchQunActivity.this)
                                .Params("groupname", charSequence.toString())
                                .Params("userGuid", user.getGuid())
                                .build()
                                .postObserverList()
                )
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe((groupBeanList) -> {
                    SearchQunActivity.this.groupBeanList = (List<SearchGroupResultBean>) groupBeanList;
                    setAdapter();
                });
    }

    private void setAdapter() {
        if (quickAdapter == null) {
            quickAdapter = new BaseQuickAdapter<SearchGroupResultBean, BaseViewHolder>(R.layout.list_item_qun, groupBeanList) {

                @Override
                protected void convert(BaseViewHolder helper, SearchGroupResultBean item) {
                    ImageView headImg = helper.getView(R.id.iv_head);
                    ImageUtil.setCircleImage(item.getGroupphoto(), headImg);
                    helper.setText(R.id.tv_name, item.getGroupname());
                    helper.setText(R.id.tv_time, item.getGroupid());
                }
            };
            dataBinding.recyclerView.setAdapter(quickAdapter);
            ItemClickSupport.addTo(dataBinding.recyclerView).setOnItemClickListener(
                    (rv, p, v) -> {
                        clickItemPosition = p;
                        alertDialog.show();

                    }
            );
        } else {
            quickAdapter.notifyDataChanged(groupBeanList, true);
        }
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {

        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        subscription.unsubscribe();
    }
}
